//
//  SystemRightJudge.m
//  mcrmSAAS
//
//  Created by yuxiuyi on 16/11/16.
//  Copyright © 2016年 GuangZhouXuanWu. All rights reserved.
//

#import "SystemRightJudge.h"

#import <AddressBook/AddressBook.h>
#import <AddressBookUI/AddressBookUI.h>

#import <AVFoundation/AVFoundation.h>

#import <AssetsLibrary/ALAsset.h>
#import <Photos/PHPhotoLibrary.h>

@implementation SystemRightJudge

+(BOOL)checkAddressBookAuthorization
{
    __block NSInteger thisResult = YES;
    
    ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, NULL);
    ABAuthorizationStatus authStatus = ABAddressBookGetAuthorizationStatus();
    
    if (authStatus != kABAuthorizationStatusAuthorized)
    {
        ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error){
            dispatch_async(dispatch_get_main_queue(), ^{
                if (error)
                    NSLog(@"Error: %@", (__bridge NSError *)error);
                else if (!granted)
                {
                    [XUIAlertView alertViewWithTitle:@"无法获取通讯录" message:@"请到“设置-隐私-通讯录”打开本应用的权限设置" cancelButtonTitle:@"确定" otherButtonTitles:nil clickBlock:^(NSInteger nClickindex) {
                    }];
                    thisResult = NO;
                }
                else
                    thisResult = YES;
            });
        });
    }
    
    return thisResult;
}

+(BOOL)checkRecordAuthorization
{
    __block BOOL bCanRecord = YES;
    
#ifdef __IPHONE_7_0
    AVAudioSession *audioSession = [AVAudioSession sharedInstance];
    if ([audioSession respondsToSelector:@selector(requestRecordPermission:)])
    {
        [audioSession performSelector:@selector(requestRecordPermission:) withObject:^(BOOL granted)
         {
            bCanRecord = granted;
        }];
    }
#endif
    
    if (!bCanRecord)
    {
        [XUIAlertView alertViewWithTitle:@"无法录音" message:@"请在“设置-隐私-麦克风”打开本应用的权限设置" cancelButtonTitle:@"确定" otherButtonTitles:nil clickBlock:^(NSInteger nClickindex)
         {
         }];
    }
    return bCanRecord;
}

+(BOOL)checkLocationAuthorization
{
    __block NSInteger thisResult = YES;
    CLAuthorizationStatus status = [CLLocationManager authorizationStatus];
    if (kCLAuthorizationStatusDenied == status || kCLAuthorizationStatusRestricted == status)
    {
        [XUIAlertView alertViewWithTitle:@"无法定位" message:@"请在“设置-隐私-定位”打开本应用的权限设置" cancelButtonTitle:@"确定" otherButtonTitles:nil clickBlock:^(NSInteger nClickindex)
         {
         }];
        thisResult = NO;
    }
    return thisResult;
}

+(BOOL)checkCameraAuthorization
{
    __block NSInteger thisResult = YES;
    
    AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
    if(authStatus == AVAuthorizationStatusRestricted || authStatus == AVAuthorizationStatusDenied)
    {
        [XUIAlertView alertViewWithTitle:@"无法拍照" message:@"请在“设置-隐私-相机”打开本应用的权限设置" cancelButtonTitle:@"确定" otherButtonTitles:nil clickBlock:^(NSInteger nClickindex)
         {
         }];
        
        thisResult = NO;
    }
    
    return thisResult;
}

+(BOOL)checkPhotoAuthorization
{
    __block NSInteger thisResult = YES;
    
#ifdef __IPHONE_8_0 
    PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus];
    if (status == PHAuthorizationStatusRestricted || status == PHAuthorizationStatusDenied)
    {
       thisResult = NO;
    }
#else
    ALAuthorizationStatus author = [ALAssetsLibrary authorizationStatus];
    if (author == kCLAuthorizationStatusRestricted || author ==kCLAuthorizationStatusDenied)
    {
        thisResult = NO;
    }
#endif
    
    if (thisResult == NO)
    {
        [XUIAlertView alertViewWithTitle:@"无法获取照片" message:@"请在“设置-隐私-照片”打开本应用的权限设置" cancelButtonTitle:@"确定" otherButtonTitles:nil clickBlock:^(NSInteger nClickindex)
         {
         }];
    }
    
    return thisResult;
}

@end
